import scrapy
import re
import logging
logger = logging.getLogger(__name__)


class BestbuySpider(scrapy.Spider):
    name = 'bestbuy'
    allowed_domains = ['bestbuy.com']
    start_urls = ['https://www.bestbuy.com/site/searchpage.jsp?_dyncharset=UTF-8&sp=-bestsellingsort%20skuidsaas&st=Headphones&intl=nosplash']

    def parse(self, response):
        url_main = "https://www.bestbuy.com"
        lis = response.xpath("//ol[@class='sku-item-list']/li[@class='sku-item']//div[@class='shop-sku-list-item']")
        for li in lis:
            # 链接
            href = url_main + li.xpath(".//a[@class='image-link']/@href").extract_first()
            SKU_ID = re.search(r"skuId=(\d+)", href).group(1)
            # 排名
            # sort_by = "#%02d" % (lis.index(li) + 1)
            # 标题 product-image
            title = li.xpath(".//h4[@class='sku-header']/a/text()").extract_first().replace("'", "")
            # 名称
            # name = title.split(" ")[0] + " " + title.split(" ")[1]
            # 图片
            img_url = li.xpath(".//a[@class='image-link']/img/@src").extract_first()
            meta_dict = {}
            meta_dict['name'] = self.name
            meta_dict['img_url'] = img_url
            meta_dict['SKU_ID'] = SKU_ID
            # meta_dict['sort_by'] = sort_by
            meta_dict['title'] = title
            yield scrapy.Request(href, callback=self.parse_detail, meta=meta_dict)
        # 下一页
        next_url = response.xpath('//a[@class="sku-list-page-next"]/@href').extract_first()
        yield scrapy.Request(next_url, callback=self.parse)

    def parse_detail(self, response):
        item = response.meta
        item['avg_score'] = response.xpath('//div[@class="row v-m-bottom-g"]//span[@class="ugc-c-review-average"]/text()').extract_first()
        print(item)
        return item
